const app = getApp();

Page({
  data: {
    order: null
  },

  onLoad(options) {
    const { id } = options;
    this.loadOrderDetail(id);
  },

  async loadOrderDetail(orderId) {
    try {
      const res = await wx.request({
        url: `${app.globalData.baseUrl}/order/${orderId}/`,
        method: 'GET',
        header: {
          'Authorization': `Bearer ${wx.getStorageSync('token')}`
        }
      });

      if (res.data.code === 200) {
        this.setData({
          order: res.data.data
        });
      }
    } catch (error) {
      wx.showToast({
        title: '加载订单详情失败',
        icon: 'none'
      });
    }
  },

  async handleCancelOrder() {
    const { order } = this.data;
    if (order.status !== 'pending') {
      wx.showToast({
        title: '只能取消待支付订单',
        icon: 'none'
      });
      return;
    }

    try {
      const res = await wx.request({
        url: `${app.globalData.baseUrl}/order/${order.id}/cancel/`,
        method: 'POST',
        header: {
          'Authorization': `Bearer ${wx.getStorageSync('token')}`
        }
      });

      if (res.data.code === 200) {
        wx.showToast({
          title: '取消成功',
          icon: 'success',
          success: () => {
            wx.navigateBack();
          }
        });
      }
    } catch (error) {
      wx.showToast({
        title: '取消订单失败',
        icon: 'none'
      });
    }
  }
}); 